package com.zkthink.ceres.promotion.service.impl;


import com.zkthink.base.R;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.promotion.dao.SalesProductLibraryMapper;
import com.zkthink.ceres.promotion.dto.SalesProductLibrarySaveDTO;
import com.zkthink.ceres.promotion.entity.SalesProductLibrary;
import com.zkthink.ceres.promotion.service.SalesProductLibraryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class SalesProductLibraryServiceImpl extends SuperServiceImpl<SalesProductLibraryMapper, SalesProductLibrary> implements SalesProductLibraryService {
    @Autowired
    private SalesProductLibraryMapper salesProductLibraryMapper;

    @Override
    public R<Boolean> addSalesProductLibrary(SalesProductLibrarySaveDTO dto) {
        List<Long> productIdLists = dto.getProductIdLists();
        if (productIdLists == null || productIdLists.size() == 0) {
            R.success();
        }
        for (Long productIdList : productIdLists) {
            SalesProductLibrary result = new SalesProductLibrary();
            SalesProductLibrary salesProductLibrary = salesProductLibraryMapper.selectByProductId(productIdList);
            result.setProductId(productIdList);
            if (salesProductLibrary == null) {
                baseMapper.insert(result);
            } else if (salesProductLibrary.getIsDelete().intValue() == 1) {
                salesProductLibrary.setIsDelete(0);
                baseMapper.updateById(salesProductLibrary);
            }
        }
        return R.success();
    }

    @Override
    public R<Boolean> deleteSalesProductLibrary(SalesProductLibrarySaveDTO dto) {
        List<Long> productIdLists = dto.getProductIdLists();
        for (Long productIdList : productIdLists) {
            SalesProductLibrary salesProductLibrary = salesProductLibraryMapper.selectByProductId(productIdList);
            salesProductLibrary.setIsDelete(1);
            baseMapper.updateById(salesProductLibrary);
        }
        return null;
    }
}
